package com.tarena.lbs.service.impl;

import com.tarena.lbs.entity.CouponCode;
import com.tarena.lbs.mapper.CouponCodeMapper;
import com.tarena.lbs.service.CouponCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CouponCodeServiceImpl implements CouponCodeService {

    @Autowired
    private CouponCodeMapper couponCodeMapper;

    @Override
    public CouponCode getCouponCodeByCode(String couponCode) {
        return couponCodeMapper.getCouponCodeByCode(couponCode);
    }

    @Override
    public List<CouponCode> listAvailableCodesByCouponId(Integer couponId, Integer count) {
        return couponCodeMapper.listAvailableCodesByCouponId(couponId, count);
    }

    @Override
    @Transactional
    public boolean updateCouponCodeStatus(Integer id, Integer status) {
        int result = couponCodeMapper.updateCouponCodeStatus(id, status);
        return result > 0;
    }

    @Override
    @Transactional
    public boolean batchInsertCouponCodes(List<CouponCode> codes) {
        int result = couponCodeMapper.batchInsertCouponCodes(codes);
        return result == codes.size();
    }

    @Override
    public String generateCouponCode() {
        return UUID.randomUUID().toString().replace("1", "0").substring(0, 16).toUpperCase();
    }
}